package com.qinggeng.project.system.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.GoodsSignin;
import com.qinggeng.project.system.service.IGoodsSigninService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 商品签到Controller
 *
 * @author qinggeng
 * @date 2020-07-15
 */
@RestController
@RequestMapping("/system/signin")
public class GoodsSigninController extends BaseController
{
    @Autowired
    private IGoodsSigninService goodsSigninService;

    /**
     * 查询商品签到列表
     */
    @PreAuthorize("@ss.hasPermi('system:signin:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSignin goodsSignin)
    {
        startPage();
        List<GoodsSignin> list = goodsSigninService.selectGoodsSigninList(goodsSignin);
        return getDataTable(list);
    }

    /**
     * 导出商品签到列表
     */
    @PreAuthorize("@ss.hasPermi('system:signin:export')")
    @Log(title = "商品签到", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsSignin goodsSignin)
    {
        List<GoodsSignin> list = goodsSigninService.selectGoodsSigninList(goodsSignin);
        ExcelUtil<GoodsSignin> util = new ExcelUtil<GoodsSignin>(GoodsSignin.class);
        return util.exportExcel(list, "signin");
    }

    /**
     * 获取商品签到详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:signin:query')")
    @GetMapping(value = "/{signinId}")
    public AjaxResult getInfo(@PathVariable("signinId") Long signinId)
    {
        return AjaxResult.success(goodsSigninService.selectGoodsSigninById(signinId));
    }

    /**
     * 新增商品签到
     */
    @PreAuthorize("@ss.hasPermi('system:signin:add')")
    @Log(title = "商品签到", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSignin goodsSignin)
    {
        if (goodsSigninService.selectGoodsSigninBySigninWeek(goodsSignin.getSigninWeek()) != null){
            return AjaxResult.error("签到日期已存在礼品");
        }
        return toAjax(goodsSigninService.insertGoodsSignin(goodsSignin));
    }

    /**
     * 修改商品签到
     */
    @PreAuthorize("@ss.hasPermi('system:signin:edit')")
    @Log(title = "商品签到", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSignin goodsSignin)
    {
        return toAjax(goodsSigninService.updateGoodsSignin(goodsSignin));
    }

    /**
     * 删除商品签到
     */
    @PreAuthorize("@ss.hasPermi('system:signin:remove')")
    @Log(title = "商品签到", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signinIds}")
    public AjaxResult remove(@PathVariable Long[] signinIds)
    {
        return toAjax(goodsSigninService.deleteGoodsSigninByIds(signinIds));
    }
}
